﻿using ControlHelper.Attributes;
using ControlHelper.WPF;
using Creator.Core;
using Creator.Core.Base;
using Creator.Core.Component; 
using HandyControl.Interactivity;
using PropertyTools.Wpf;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; 

namespace DesktopCreator.Controls
{
    /// <summary>
    /// UCComponent.xaml 的交互逻辑
    /// </summary>
    public partial class UCComponent : UserControl
    {
        public UCComponent()
        {
            InitializeComponent();
            Loaded += (s, e) =>
            {
                sp.Visibility = Visibility.Collapsed;
            };
            toggleButton.Click += (s, e) =>
            {
                var check = (bool)toggleButton.IsChecked;
                sp.Visibility = check ? Visibility.Visible : Visibility.Collapsed;
                toggleButton.Content = check ? " ˅ " : " ˃ ";
            };
            cbActive.Checked += CbActive_Checked;
            cbActive.Unchecked += CbActive_Checked;
        }



        public UCComponent(Behaviour behaviour) : this()
        {
            Behaviour = behaviour ?? throw new ArgumentNullException($"{nameof(behaviour)} 为空！");
            SetBasicData(behaviour);
          
            dynamic fe = null;
            if (behaviour is ActionList al)
            {
                fe = new UCActions(al.Actions,behaviour.BaseObject);
            }
            else
            {
                //自动解析这个组件，将可编辑的内容显示在界面。
                temp = new ClassControl(behaviour.GetType(), true, behaviour);
                temp.NewData += (data) =>
                {
                    if (data is Behaviour b)
                    {
                        SetBasicData(b); 
                    }
                }; 
                //PropertyGrid pg = new PropertyGrid();
                //pg.SelectedObject = behaviour;
                //pg.ControlFactory = new ControlFactory();

                tempTemp = temp;
                //如果这个组件需要引用其他组件时，则注册组件搜寻事件
                temp.OnBehaviourClick += Cc_OnBehaviourClick;
                fe = temp;
            }
            //组件可以在编辑界面加载，则注册方法
            if (behaviour is IComponent component)
            {
                component.OnBehaviourClick += Cc_OnBehaviourClick;
                if (component.StartOnEditor)
                {
                    component.OnEditorEvent += ClickOnEditor;
                    MiEditorData.Visibility = Visibility.Visible;
                }
            }

            if(behaviour is RenderComponent render)
            {
                Editor.AddRenderComponent?.Invoke(render);
            }



            sp.Children.Add(fe);
        }
        private ClassControl temp;
        private FrameworkElement tempTemp;
        public event Action<FrameworkElement> OnRemoved;
        public string Title
        {
            get => txtTitle.Text; private set
            {
                txtTitle.Text = value;
            }
        }

        public Behaviour Behaviour { get; private set; }



        private void CbActive_Checked(object sender, RoutedEventArgs e)
        {
            Behaviour.Active = (bool)cbActive.IsChecked;
        }
        /// <summary>
        /// 选择指定类型的组件
        /// </summary>  
        private IComponent Cc_OnBehaviourClick(object sender, Type type)
        {
            var ucd = new UCDropList()
            {
                HorizontalAlignment= HorizontalAlignment.Stretch, 
                VerticalAlignment= VerticalAlignment.Stretch,
            };
            IComponent temp = null;
            ucd.CreateAllRunTypes(type);
            Window window = new Window();
            window.Title = $"选择对应的类型组件,组件路径[{type.GetCustomAttribute<ComponentMenuAttribute>()?.Path}],类型[{type.Name}]";
            ucd.OnCreate += (o) =>
            {
                temp = o as IComponent;
                window.Close();
            }; 
            window.Content = ucd;
            window.ShowDialog();

            return temp;
        }
         
        private void SetBasicData(Behaviour behaviour)
        {
            Title = behaviour.Name;
            cbActive.IsChecked = behaviour.Active;
        }
        /// <summary>
        /// 打开大窗口编辑
        /// </summary> 
        private void MiOpenWindow_Click(object sender, RoutedEventArgs e)
        {
            OpenWindow();
        }
        private void OpenWindow()
        {
            if (tempTemp == null) return;

            sp.Children.Remove(tempTemp);
            Window window = new Window();
            ScrollViewer sv = new ScrollViewer();
            sv.Content = tempTemp;
            window.Content = sv;
            //temp.ShowAll();
            window.ShowDialog();
            sv.Content = null;
            sp.Children.Add(tempTemp);
        }
        /// <summary>
        /// 移除组件
        /// </summary> 
        private void MiRemove_Click(object sender, RoutedEventArgs e)
        {
            var dialog = MessageBox.Show("是否确认删除这个组件", "注意", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (dialog != MessageBoxResult.Yes) return;
            Behaviour.BaseObject.RemoveBehaviour(Behaviour);
            OnRemoved?.Invoke(this);
            Editor.RemoveRenderComponent?.Invoke(this.Behaviour as RenderComponent);
        }
        private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if(e.ClickCount == 2)
            {
                OpenWindow();
            }
        }

        //------------------ 绘制

        private void ClickOnEditor(object sender, object data)
        {
            if(data is IList list)
            {
               Editor.DrawData(list);
            }
        }

        private void Mi_ClickGetData(object sender, RoutedEventArgs e)
        {
            if(Behaviour is IComponent component)
            {
                component.OnEditor();
            }
        }

    
    }
}
